package controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import entity.Computerroom;
import service.ComputerRoomService;
import utils.Page;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/main/computerroom")
public class ComputerRoomController extends BaseController{
    @Autowired
    private ComputerRoomService computerRoomService;
    @RequestMapping("/page")
    public String page(Model model, Computerroom condition,
                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize
            , HttpSession httpSession) {
        Page<Computerroom> page = null;
        try {
            // 查询用户的分页信息
            page = computerRoomService.getComputerRooms(condition, pageNo, pageSize);
            // 将分页数据、角色列表和查询条件设置到模型数据中
            model.addAttribute("pageInfo", page);
            model.addAttribute("condition", condition);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "computerroom/page";
    }
    @GetMapping("/save")
    public String save(){
        return "computerroom/save";
    }
    @PostMapping("/save")
    public String save(Computerroom computerroom){
        boolean ok = false;
        try {
            if (computerroom.getRoomId() == null) {
                ok = computerRoomService.insert(computerroom);
            } else {
                ok = computerRoomService.update(computerroom);
            }
            if (ok) {
                // 保存成功，跳回列表页面
                return redirect("/main/computerroom/page");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "/computerroom/save";
    }
    @DeleteMapping("/del/{roomId}")
    @ResponseBody
    public Object del(@PathVariable("roomId") Integer id, Model model) {
        Map<String, Object> map = new HashMap<>();
        try {
            boolean num = computerRoomService.delete(id);
            if (num) {
                map.put("code", 200);
                map.put("msg", "删除成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return map;
    }
    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Long id, Model model) {

        Computerroom computerroom = null;
        try {
            computerroom = computerRoomService.getById(id);
            // 回显数据时需要 sysUser，这里给个空对象避免出错
            model.addAttribute("computerroom", computerroom);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "computerroom/save";
    }
    @ResponseBody
    @GetMapping("/{id}/view")
    public Object ajaxView(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            Computerroom computerroom = computerRoomService.getById(id);
            if (computerroom != null) {
                map.put("code", 200);
                map.put("data", computerroom);
            } else {
                map.put("code", 500);
            }
        } catch (Exception e) {
            map.put("code", 500);
        }
        return map;
    }
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, Model model) {
        Computerroom computerroom = null;
        try {
            computerroom = computerRoomService.getById(id);
            model.addAttribute("computerroom", computerroom);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "computerroom/view";
    }
    @ResponseBody
    @GetMapping("/accountExist")
    public Object accountExist(@RequestParam("roomName") String account) {
        Map<String, Object> map = new HashMap<>();
        try {
            // 角色编号不可为空
            if (StringUtils.isEmpty(account)) {
                map.put("exist", 0);
            } else {
                Computerroom computerroom = computerRoomService.findByAccount(account);
                if (null != computerroom) {
                    map.put("exist", 1);
                } else {
                    map.put("exist", 0);
                }
            }
        } catch (Exception e) {
            map.put("error", "出现异常，请联系管理员处理！ ");
        }
        return map;
    }
}
